<?php

class Http{
    protected $baseUrl;
    protected $method;
    protected $header;
    protected $body;

    protected $proxy;

    protected $opts = [];

    public function request($url){
        $curl = curl_init($url);
        curl_setopt_array($curl, $this->opts);
        $data = curl_exec($curl);
        curl_close($curl);
        return json_decode($data, true);
    }

    public function setTimeout(int $time)
    {
        $this->opts[CURLOPT_TIMEOUT] = $time;
        return $this;
    }

    public function setPostData(array $data, $isJson = false){
        $http = $isJson ? json_encode($data): http_build_query($data);
        $this->opts[CURLOPT_POST] = 1;
        $this->opts[CURLOPT_POSTFIELDS] = $http;
        return $this;
    }

    public function setHeader($header){
        $this->opts[CURLOPT_HTTPHEADER] = $header;
        return $this;
    }

    public function setProxy($proxy){
        $this->opts[CURLOPT_PROXY] = $proxy;
        return $this;
    }
}